package com.test.ssh.webapp.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;

import com.opensymphony.xwork2.ActionContext;
import com.test.ssh.model.MessageDispatch;
import com.test.ssh.model.SiteMessage;
import com.test.ssh.model.User;
import com.test.ssh.service.GenericManager;
import com.test.ssh.service.UserManager;

@Results(
		{
			@Result(name="inbox",value="/message/inboxMessage.jsp"),
			@Result(name="sentbox",value="message/sentboxMessage.jsp"),
			@Result(name="newMessageForm",value="/message/messageForm.jsp"),
			@Result(name="replyForm",value="/message/messageForm.jsp"),
			@Result(name="sendMessageOk",value="/message/sendMessageOk.jsp"),
			@Result(name="messageView",value="/message/messageForm.jsp"),
			@Result(name="resendMessageForm",value="/message/messageForm.jsp")
		}
		)

public class MessageAction {
	
	private UserManager userManager ;
	GenericManager<SiteMessage,Long> siteMessageManager ;
	GenericManager<MessageDispatch,Long> messageDispatchManager ;
	private SiteMessage siteMessage ;
	private MessageDispatch messageDispatch ;
	private List<MessageDispatch> messageDispatches ;
	private List<SiteMessage> messages;
	
	
	public String execute() {
		return listInbox() ;
	}
	//显示收件箱
	public String listInbox() {
		String hql="from MessageDispatch where receiver.id=? order by id desc" ;
		messageDispatches=messageDispatchManager.findByHql(hql, new Object[] {this.getCurrentUser().getId()}) ;
		return "inbox" ;
	}
	
	public void prepare() throws Exception {
		String siteMessageIdStr = ServletActionContext.getRequest().getParameter("siteMessage.id");
		if(siteMessageIdStr != null && !"".equals(siteMessageIdStr)){
			siteMessage = siteMessageManager.get(Long.parseLong(siteMessageIdStr));
		}
		String messageDispatchIdStr = ServletActionContext.getRequest().getParameter("messageDispatch.id");
		if(messageDispatchIdStr != null && !"".equals(messageDispatchIdStr)){
			messageDispatch = messageDispatchManager.get(Long.parseLong(messageDispatchIdStr));
		}
	}
	
	public String readMessage() {
		messageDispatch.setStatus("readed") ;
		messageDispatchManager.save(messageDispatch) ;
		return "messageView" ;
	}
	
	public String sentbox() {
		messages = siteMessageManager.findByHql("from SiteMessage where sender=?", getCurrentUser()) ;
		//System.out.println(messages.size());
		return "sentbox" ;
	}
	
	//新增消息表单
	public String newMessage() {
		return "newMessageForm" ;
	}
	
	public String resendMessageForm() {
		return "resendMessageForm" ;
	}
	
	//回复
	public String reply() {
		SiteMessage message = new SiteMessage() ;
		message.setTitle("To:"+ siteMessage.getTitle()) ;
		message.setReceiverNames(siteMessage.getSender().getUsername()) ;
		siteMessage=message ;
		if("new".equals(messageDispatch.getStatus())) {
			messageDispatch.setStatus("readed") ;
			messageDispatchManager.save(messageDispatch) ;
		}
		return "replyForm" ;
	}
	
	
	public String sendMessage(){
		Date now = new Date();
		String receiverNames = siteMessage.getReceiverNames();
		siteMessage.setCreationDate(now);
		siteMessage.setSender(getCurrentUser());
		siteMessage = siteMessageManager.save(siteMessage);
		String[] namesArr = receiverNames.split(",");
		for (int i = 0; i < namesArr.length; i++) {
			String name = namesArr[i];
			User u = userManager.findUniqueBy(User.class, "username", name);
			MessageDispatch dispatch = new MessageDispatch();
			dispatch.setMessage(siteMessage);
			dispatch.setReceiver(u);
			dispatch.setStatus("new");
			messageDispatch = messageDispatchManager.save(dispatch);
		}
		if(messageDispatch != null && messageDispatch.getId() != null){
			messageDispatch.setStatus("replied");
			messageDispatch = messageDispatchManager.save(messageDispatch);
		}
		return "sendMessageOk";
	}
	private User getCurrentUser() {
		// TODO Auto-generated method stub
		return (User)((Map)ActionContext.getContext().get("session")).get("currentUser") ;
	}
	public UserManager getUserManager() {
		return userManager;
	}
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	public GenericManager<SiteMessage, Long> getSiteMessageManager() {
		return siteMessageManager;
	}
	public void setSiteMessageManager(
			GenericManager<SiteMessage, Long> siteMessageManager) {
		this.siteMessageManager = siteMessageManager;
	}
	public GenericManager<MessageDispatch, Long> getMessageDispatchManager() {
		return messageDispatchManager;
	}
	public void setMessageDispatchManager(
			GenericManager<MessageDispatch, Long> messageDispatchManager) {
		this.messageDispatchManager = messageDispatchManager;
	}
	public SiteMessage getSiteMessage() {
		return siteMessage;
	}
	public void setSiteMessage(SiteMessage siteMessage) {
		this.siteMessage = siteMessage;
	}
	public MessageDispatch getMessageDispatch() {
		return messageDispatch;
	}
	public void setMessageDispatch(MessageDispatch messageDispatch) {
		this.messageDispatch = messageDispatch;
	}
	public List<MessageDispatch> getMessageDispatches() {
		return messageDispatches;
	}
	public void setMessageDispatches(List<MessageDispatch> messageDispatches) {
		this.messageDispatches = messageDispatches;
	}
	public List<SiteMessage> getMessages() {
		return messages;
	}
	public void setMessages(List<SiteMessage> messages) {
		this.messages = messages;
	}
	
	
}
